﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Workflow.ComponentModel;
using Microsoft.IdentityManagement.WebUI.Controls;
using Microsoft.ResourceManagement.Workflow.Activities;
using FIMActivitiesLibrary;

namespace WebUI
{
    struct myToolTip
    {
        public const string activityName = "The name of the activity, will be used in eventlog";
        public const string readResourceActorIDParameter = "Used to specify the actorID to use for reading a resource";
        public const string readResourceIDParameter = "Used to specify witch resource to read from, If not specified the default from current request is used";
        public const string remoteServerName = "Specify the servername on witch the powershell should be executed on, if not specified local execution is used";
        public const string PSCredentialUserName = "Specifies a powershell credential username";
        public const string PSCredentialPassword = "Specifies a powershell credential password";
        public const string UserName = "Specifies a remote username";
        public const string UserPassword = "Specifies a remote password";
        public const string powershellCmdLetPlugins = "Specify powershell plugins to be used by script.";
        public const string powershellCmdLetScript = "Enter the powershell cmdlet script.";
        public const string powershellParameters = "Syntax : {Parametername} [//Target|RequestCreate|RequestInsert|RequestRemove|RequestModify|Environment/Attribute|Value] or [//Credentials/UserName|UserDomain|Password]";
        public const string updateResourceActorIDParameter = "Syntax : {PS ParameterName} | GUID | [//Target|WorkflowData|RequestInsert|RequestRemove/{AttributeName}] Enter the workflow actorID guid to use for updating resources, Enter blank to use current requestor.";
        public const string updateResourceIDParameter = "Syntax : {PS ParameterName} | GUID | [//Target|WorkflowData|RequestInsert|RequestRemove/{AttributeName}], Used to select witch target resource should be updated, Enter blank to use current target.";
        public const string updateResourceAttributeParameters = "Syntax : {PS ParameterName} | GUID | [//Target|WorkflowData|TargetInsert|TargetRemove|TargetSet/{AttributeName}], Used to select witch target resource should be updated, Enter blank to use current target.";
        public const string eventLogName = "Specify Eventlog name to write eventlog entries into";
        public const string AuthenticationMechanism = "Select AuthenticationMechanism to use for remote logon";
    }

    struct myDefaultValues
    {
        public const string activityName = ";Specify the name of the activity";
        public const string readResourceActorID = ";Blank for current actor | {guid} | [//WorkflowData/{AttributeName}]";
        public const string readResourceIDParameter = ";Blank for current target | {guid} | WorkflowData/{AttributeName}]";
        public const string remoteServerName = ";Specify remote server for remote execution or leave blank for local server.";
        public const string UserName = ";Specify the remote domain account (Remote powershell only).";
        public const string PSCredentialUsername = ";Specify the domain account (Powershell credentials only).";
        public const string powershellCmdLetPlugins = ";Enter the powershell plugins to use.";
        public const string powershellCmdLetScript = ";Enter the powershell cmdlet script./n;PARAM($FirstName,$LastName)/n;/n;$Result = @{}/n;$Result.Add('DisplayName',$FirstName + ',' +$LastName)/n;/n;$Result";
        public const string powershellParameters = ";Specify the parameters for the powershell cmdlet script example:[//Target/FirstName], [RequestInsert/Roles], [//RequestCreate/LastName], [//Environment/Computername]";
        public const string updateResourceActorID = ";Blank for current actor | {guid} | [//WorkflowData/{AttributeName}]";
        public const string updateResourceID = ";{PS ParameterName} | GUID | [//Target|WorkflowData|RequestInsert|RequestRemove/{AttributeName}]";
        public const string updateResourceAttributeParameters = ";{PS ParameterName} | GUID | [//Target|WorkflowData|RequestInsert|RequestRemove/{AttributeName}]";
        public const string eventLogName = ";{FIM.Powershell.Extensions}";
        public const string AuthenticationMechanism = "Default";
    }

    class PowershellActivitySettingsPart : ActivitySettingsPart
    {
        #region WebUIparameters

        //Enable or Disable the activity
        const String checkboxEnableActivity = "checkboxEnableActivity";
        const String dataEnableActivity = "dataEnableActivity";
        //Gives the activity a name
        const String txtActivityName = "txtActivityName";
        const String dataActivityName = "dataActivityName";
        //Specifies the unresolved parameters to pass to the powershell
        //During the workflow this will be resolved to actual values before passed to powershell
        const String txtPowershellParameters = "txtPowershellParameters";
        const String dataPowershellParameters = "dataPowershellParameters";
        //Specifies the unresolved parameters for update the target/workflowdata attributes
        //This will be resolved with result from powershell script
        const String txtTargetUpdateParameters = "txtTargetUpdateParameters";
        const String dataTargetUpdateParameters = "dataTargetUpdateParameters";
        //Specifies the unresolved guid to run under ActorID context on read
        const String txtReadTargetActorID = "txtReadTargetActorID";
        const String dataReadTargetActorID = "dataReadTargetActorID";
        //Specifies the unresolved guid run under ActorID context on update
        const String txtActorID_Update = "txtActorID_Update";
        const String dataActorID_Update = "dataActorID_Update";
        //Specifies the powershell script directly in configuration
        const String txtPowershellCmdLetScript = "txtPowershellCmdLetScript";
        const String dataPowershellCmdLetScript = "dataPowershellCmdLetScript";
        //Specifies a file to load powershell script from
        const String txtPowershellCmdLetFile = "txtPowershellCmdLetFile";
        const String dataPowershellCmdLetFile = "dataPowershellCmdLetFile";
        //Specifies witch powershell plugins to load
        const String txtPowershellCmdLetPlugins = "txtPowershellPlugins";
        const String dataPowershellCmdLetPlugins = "dataPowershellPlugins";
        //Specifies if the powershell should run on remote server
        const String txtRemoteServer = "txtRemoteServer";
        const String dataRemoteServer = "dataRemoteServer";
        //Specifies the user to use on remote server
        const String txtUserName = "txtUserName";
        const String dataUserName = "dataUserName";
        //Specifies the password to useon remote server
        const String txtUserPassword = "txtUserPassword";
        const String dataUserPassword = "dataUserPassword";
        //Specifies the PS Credential to use in script
        const String txtPSCredentialUserName = "txtPSCredentialUserName";
        const String dataPSCredentialUserName = "dataPSCredentialUserName";
        //Specifies the password to useon remote server
        const String txtPSCredentialPassword = "txtPSCredentialPassword";
        const String dataPSCredentialPassword = "dataPSCredentialPassword";
        //Specifies the unresolved GUID to Resource on read resource, used if a different resource should be used insted of current
        const String txtReadTargetResourceIdParameter = "txtTargetResourceIdReadParameter";
        const String dataReadTargetResourceIdParameter = "dataTargetResourceIdReadParameter";
        //Specifies the unresolved GUID to Resource on resource update, used if a different resource should be used insted of current
        const String txtUpdateTargetResourceIdParameter = "txtTargetResourceIdParameter";
        const String dataUpdateTargetResourceIdParameter = "dataTargetResourceIdParameter";
        //Specifies the logging level
        const String txtLoggingLevel = "txtLoggingLevel";
        const String dataLoggingLevel = "dataLoggingLevel";
        //Specifies the eventlog name
        const String txteventLogName = "txteventLogName";
        const String dataeventLogName = "dataeventLogName";
        //Specifies the AuthenticationMechanism
        const String txtAuthenticationMechanism = "txtAuthenticationMechanism";
        const String dataAuthenticationMechanism = "dataAuthenticationMechanism";
        //Specifies ExecutionModes
        const String txtExecutionMode = "txtExecutionMode";
        const String dataExecutionMode = "dataExecutionMode";

        #endregion

        private static ArrayList loggingLevels = new ArrayList(new string[] { "0 - No logging", "1 - Minimum logging", "2 - Medium logging", "3 - High logging", "4 - Debug" });
        private static ArrayList errorRetryOption = new ArrayList(new string[] { "Create runspace", "Create runspace, Execute powershell", "Create runspace, Powershell retry response" });
        private static ArrayList AuthenticationMechanisms = new ArrayList(new string[] { "Default", "Credssp", "Basic","Digest", "Kerberos", "Negotiate", "NegotiateWithImplicitCredential" });

        // Called when a user clicks the Save button in the Workflow Designer. 
        // Returns an instance of the RequestPowershellActivity class that 
        // has its properties set to the values entered into the text box controls
        // used in the UI of the activity. 
        public override System.Workflow.ComponentModel.Activity GenerateActivityOnWorkflow(SequentialWorkflow workflow)
        {
            if (!this.ValidateInputs()) { return null; }
            PowershellActivity psActivity = new PowershellActivity();

            psActivity.enableActivity = this.GetCheckBox(checkboxEnableActivity);
            psActivity.activityName = this.GetText(txtActivityName);
            psActivity.targetUpdateParameters = this.GetText(txtTargetUpdateParameters);
            psActivity.powershellParameters = this.GetText(txtPowershellParameters);
            psActivity.ReadTargetActorID = this.GetText(txtReadTargetActorID);
            psActivity.ActorID_Update = this.GetText(txtActorID_Update);
            psActivity.PowershellCmdLetScript = this.GetText(txtPowershellCmdLetScript);
            psActivity.powershellCmdLetPlugins = this.GetText(txtPowershellCmdLetPlugins);
            psActivity.RemoteServer = this.GetText(txtRemoteServer);
            psActivity.UserName = this.GetText(txtUserName);
            psActivity.UserPassword = this.GetText(txtUserPassword);
            psActivity.PSCredentialUserName = this.GetText(txtPSCredentialUserName);
            psActivity.PSCredentialPassword = this.GetText(txtPSCredentialPassword);
            psActivity.readTargetResourceIdParameter = this.GetText(txtReadTargetResourceIdParameter);
            psActivity.updateTargetResourceIdParameter = this.GetText(txtUpdateTargetResourceIdParameter);
            psActivity.loggingLevel = this.GetDropDownListSelectedIndex(txtLoggingLevel);
            psActivity.eventLogName = this.GetText(txteventLogName);
            psActivity.AuthenticationMechanism = this.GetDropDownListText(txtAuthenticationMechanism);
            return psActivity;
        }

        // Called when editing the workflow activity settings.
        public override void LoadActivitySettings(System.Workflow.ComponentModel.Activity activity)
        {

            PowershellActivity psActivity = (PowershellActivity)activity;

            if (psActivity != null)
            {
                this.SetCheckBox(checkboxEnableActivity, psActivity.enableActivity);
                this.SetText(txtActivityName, psActivity.activityName);
                this.SetText(txtTargetUpdateParameters, psActivity.targetUpdateParameters);
                this.SetText(txtUpdateTargetResourceIdParameter, psActivity.updateTargetResourceIdParameter);
                this.SetText(txtPowershellParameters, psActivity.powershellParameters);
                this.SetText(txtReadTargetActorID, psActivity.ReadTargetActorID);
                this.SetText(txtActorID_Update, psActivity.ActorID_Update);
                this.SetText(txtPowershellCmdLetScript, psActivity.PowershellCmdLetScript);
                this.SetText(txtPowershellCmdLetPlugins, psActivity.powershellCmdLetPlugins);
                this.SetText(txtRemoteServer, psActivity.RemoteServer);
                this.SetText(txtUserName, psActivity.UserName);
                this.SetText(txtUserPassword, psActivity.UserPassword);
                this.SetText(txtPSCredentialUserName, psActivity.PSCredentialUserName);
                this.SetText(txtPSCredentialPassword, psActivity.PSCredentialPassword);
                this.SetText(txtReadTargetResourceIdParameter, psActivity.readTargetResourceIdParameter);
                this.SetDropDownListIndex(txtLoggingLevel, psActivity.loggingLevel);
                this.SetText(txteventLogName, psActivity.eventLogName);
                this.SetDropDownListText(txtAuthenticationMechanism, psActivity.AuthenticationMechanism);
            }
        }

        // Saves the activity settings.
        public override ActivitySettingsPartData PersistSettings()
        {
            ActivitySettingsPartData data = new ActivitySettingsPartData();

            data[dataEnableActivity] = this.GetCheckBox(checkboxEnableActivity);
            data[dataActivityName] = this.GetText(txtActivityName);
            data[dataTargetUpdateParameters] = this.GetText(txtTargetUpdateParameters);
            data[dataUpdateTargetResourceIdParameter] = this.GetText(txtUpdateTargetResourceIdParameter);
            data[dataPowershellParameters] = this.GetText(txtPowershellParameters);
            data[dataReadTargetActorID] = this.GetText(txtReadTargetActorID);
            data[dataActorID_Update] = this.GetText(txtActorID_Update);
            data[dataPowershellCmdLetScript] = this.GetText(txtPowershellCmdLetScript);
            data[dataPowershellCmdLetPlugins] = this.GetText(txtPowershellCmdLetPlugins);
            data[dataRemoteServer] = this.GetText(txtRemoteServer);
            data[dataUserName] = this.GetText(txtUserName);
            data[dataUserPassword] = this.GetText(txtUserPassword);
            data[dataPSCredentialUserName] = this.GetText(txtPSCredentialUserName);
            data[dataPSCredentialPassword] = this.GetText(txtPSCredentialPassword);
            data[dataReadTargetResourceIdParameter] = this.GetText(txtReadTargetResourceIdParameter);
            data[dataLoggingLevel] = this.GetDropDownListText(txtLoggingLevel);
            data[dataeventLogName] = this.GetText(txteventLogName);
            data[dataAuthenticationMechanism] = this.GetDropDownListText(txtAuthenticationMechanism);
            return data;
        }

        //Restores the activity settings in the UI
        public override void RestoreSettings(ActivitySettingsPartData data)
        {

            if (data != null)
            {
                this.SetCheckBox(checkboxEnableActivity, (bool)data[dataEnableActivity]);
                this.SetText(txtActivityName, (String)data[dataActivityName]);
                this.SetText(txtTargetUpdateParameters, (String)data[dataTargetUpdateParameters]);
                this.SetText(txtReadTargetResourceIdParameter, (String)data[dataReadTargetResourceIdParameter]);
                this.SetText(txtUpdateTargetResourceIdParameter, (String)data[dataUpdateTargetResourceIdParameter]);
                this.SetText(txtPowershellParameters, (String)data[dataPowershellParameters]);
                this.SetText(txtReadTargetActorID, (String)data[dataReadTargetActorID]);
                this.SetText(txtActorID_Update, (String)data[dataActorID_Update]);
                this.SetText(txtPowershellCmdLetScript, (String)data[dataPowershellCmdLetScript]);
                this.SetText(txtPowershellCmdLetPlugins, (String)data[dataPowershellCmdLetPlugins]);
                this.SetText(txtRemoteServer, (String)data[dataRemoteServer]);
                this.SetText(txtUserName, (String)data[dataUserName]);
                this.SetText(txtUserPassword, (String)data[dataUserPassword]);
                this.SetText(txtPSCredentialUserName, (String)data[dataPSCredentialUserName]);
                this.SetText(txtPSCredentialPassword, (String)data[dataPSCredentialPassword]);
                this.SetDropDownListText(txtLoggingLevel, (String)data[dataLoggingLevel]);
                this.SetText(txteventLogName, (String)data[dataeventLogName]);
                this.SetDropDownListText(txtAuthenticationMechanism, (String)data[dataAuthenticationMechanism]);
            }
        }

        //Switches the activity between read only and read/write mode
        public override void SwitchMode(ActivitySettingsPartMode mode)
        {
            bool readOnly = (mode == ActivitySettingsPartMode.View);
            this.SetTextBoxReadOnlyOption(txtTargetUpdateParameters, readOnly);
            this.SetTextBoxReadOnlyOption(txtReadTargetResourceIdParameter, readOnly);
            this.SetTextBoxReadOnlyOption(txtUpdateTargetResourceIdParameter, readOnly);
            this.SetTextBoxReadOnlyOption(txtPowershellParameters, readOnly);
            this.SetTextBoxReadOnlyOption(txtReadTargetActorID, readOnly);
            this.SetTextBoxReadOnlyOption(txtActorID_Update, readOnly);
            this.SetTextBoxReadOnlyOption(txtPowershellCmdLetScript, readOnly);
            this.SetTextBoxReadOnlyOption(txtPowershellCmdLetPlugins, readOnly);
            this.SetTextBoxReadOnlyOption(txtRemoteServer, readOnly);
            this.SetTextBoxReadOnlyOption(txtUserName, readOnly);
            this.SetTextBoxReadOnlyOption(txtUserPassword, readOnly);
            this.SetTextBoxReadOnlyOption(txtPSCredentialUserName, readOnly);
            this.SetTextBoxReadOnlyOption(txtPSCredentialPassword, readOnly);
            this.SetTextBoxReadOnlyOption(txteventLogName, readOnly);
        }

        //Returns the activity name.
        public override string Title
        {
            get { return "Lumagate Powershell Activity - " + this.GetText(txtActivityName); }
        }

        // In general, this method should be used to validate information entered
        //  by the user when the activity is added to a workflow in the Workflow
        //  Designer.
        //  We could add code to verify that the log file path already exists on
        //  the server that is hosting the FIM Portal and check that the activity
        //  has permission to write to that location. However, the code
        //  would only check if the log file path exists when the
        //  activity is added to a workflow in the workflow designer. This class
        //  will not be used when the activity is actually run.
        //  For this activity we will just return true.
        public override bool ValidateInputs()
        {
            return true;
        }

        ///  Creates a Table that contains the controls used by the activity UI
        ///  in the Workflow Designer of the FIM portal. Adds that Table to the
        ///  collection of Controls that defines each activity that can be selected
        ///  in the Workflow Designer of the FIM Portal. Calls the base class of 
        ///  ActivitySettingsPart to render the controls in the UI.
        protected override void CreateChildControls()
        {

            Table controlLayoutTable;
            controlLayoutTable = new Table();

            //Width is set to 100% of the control size
            controlLayoutTable.Width = Unit.Percentage(100.0);
            controlLayoutTable.BorderWidth = 0;
            controlLayoutTable.CellPadding = 2;

            //Add a TableRow for each textbox in the UI 
            controlLayoutTable.Rows.Add(this.AddCheckBox("Enable Activity", checkboxEnableActivity, 20, true, "Enables the execution of activity."));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Activity name", txtActivityName, 400, 100, false, myDefaultValues.activityName, false, myToolTip.activityName,Color.Red));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Read TargetActorID parameter", txtReadTargetActorID, 400, 100, false, myDefaultValues.readResourceActorID, false, myToolTip.readResourceActorIDParameter,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Read TargetResourceID parameter", txtReadTargetResourceIdParameter, 400, 100, false, myDefaultValues.readResourceIDParameter, false, myToolTip.readResourceIDParameter,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Remote server", txtRemoteServer, 400, 100, false, myDefaultValues.remoteServerName, false, myToolTip.remoteServerName,Color.White));
            controlLayoutTable.Rows.Add(this.AddDropdownList("Remote AuthenticationMechanism", txtAuthenticationMechanism, 400, AuthenticationMechanisms));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("User Name", txtUserName, 400, 100, false, myDefaultValues.UserName, false, myDefaultValues.UserName,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("User Password", txtUserPassword, 400, 100, false, null, true, myToolTip.UserPassword,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("PSCredential User Name", txtPSCredentialUserName, 400, 100, false, myDefaultValues.PSCredentialUsername, false, myDefaultValues.PSCredentialUsername,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("PSCredential User Password", txtPSCredentialPassword, 400, 100, false, null, true, myToolTip.PSCredentialPassword,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Powershell parameters", txtPowershellParameters, 400, 500, true, myDefaultValues.powershellParameters, false, myToolTip.powershellParameters,Color.Red));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Powershell script", txtPowershellCmdLetScript, 400, 500, true, myDefaultValues.powershellCmdLetScript.Replace("/n", Environment.NewLine), false, myToolTip.powershellCmdLetScript,Color.Red));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Powershell plugins", txtPowershellCmdLetPlugins, 400, 100, false, myDefaultValues.powershellCmdLetPlugins, false, myToolTip.powershellCmdLetPlugins,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("UpdateTargetActorID parameter", txtActorID_Update, 400, 100, false, myDefaultValues.updateResourceActorID, false, myToolTip.updateResourceActorIDParameter,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("UpdateTargetResourceID parameter", txtUpdateTargetResourceIdParameter, 400, 100, false, myDefaultValues.updateResourceID, false, myToolTip.updateResourceIDParameter,Color.White));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Update Target Parameters", txtTargetUpdateParameters, 400, 500, true, myDefaultValues.updateResourceAttributeParameters, false, myToolTip.updateResourceAttributeParameters,Color.White));
            controlLayoutTable.Rows.Add(this.AddDropdownList("Logging level", txtLoggingLevel, 400, loggingLevels));
            controlLayoutTable.Rows.Add(this.AddTableRowTextBox("Eventlog Name", txteventLogName, 400, 100, false, myDefaultValues.eventLogName, false, myToolTip.eventLogName,Color.White));

            this.Controls.Add(controlLayoutTable);
            base.CreateChildControls();
        }

        #region "WebUI"

        private TableRow AddCheckBox(string lableText, string controlID, int with, bool value, string ToolTip)
        {
            TableRow row = new TableRow();
            TableCell labelCell = new TableCell();
            TableCell controllCell = new TableCell();
            Label oLabel = new Label();
            CheckBox oCheckBox = new CheckBox();

            if (ToolTip != null) { controllCell.ToolTip = ToolTip; }
            labelCell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;

            oLabel.Text = lableText;
            oLabel.CssClass = this.LabelCssClass;
            labelCell.Controls.Add(oLabel);

            oCheckBox.ID = controlID;
            oCheckBox.CssClass = this.TextBoxCssClass;
            oCheckBox.Checked = value;
            oCheckBox.Width = with;

            controllCell.Controls.Add(oCheckBox);

            row.Cells.Add(labelCell);
            row.Cells.Add(controllCell);

            return row;

        }

        private TableRow AddTableRowTextBox(String labelText, String controlID, int width, int
                                     maxLength, Boolean multiLine, String defaultValue, bool hideText, String toolTip, Color backColor)
        {
            TableRow row = new TableRow();
            TableCell labelCell = new TableCell();
            TableCell controlCell = new TableCell();
            Label oLabel = new Label();
            TextBox oText = new TextBox();

            if (!String.IsNullOrEmpty(toolTip)) oText.ToolTip = toolTip;
            if (hideText) oText.TextMode = TextBoxMode.Password;
            oLabel.Text = labelText.Replace("\n","<br>");
            oLabel.CssClass = base.LabelCssClass;
            labelCell.Controls.Add(oLabel);
            oText.ID = controlID;
            oText.CssClass = base.TextBoxCssClass;
            if (!String.IsNullOrEmpty(defaultValue)) oText.Text = defaultValue;
            oText.MaxLength = maxLength;
            oText.Width = width;
            //oText.BackColor = backColor ;
            oText.BorderColor = backColor;
            if (multiLine)
            {
                oText.TextMode = TextBoxMode.MultiLine;
                oText.Rows = System.Math.Min(6, (maxLength + 60) / 60);
                oText.Wrap = true;
            }
            controlCell.Controls.Add(oText);
            row.Cells.Add(labelCell);
            row.Cells.Add(controlCell);
            return row;
        }

        private TableRow AddDropdownList(String labelText, String controlID, int width, ArrayList values)
        {
            TableRow row = new TableRow();
            TableCell labelCell = new TableCell();
            TableCell controlCell = new TableCell();
            Label oLabel = new Label();
            DropDownList oDropDownList = new DropDownList();

            oLabel.Text = labelText;
            oLabel.CssClass = base.LabelCssClass;
            labelCell.Controls.Add(oLabel);

            oDropDownList.ID = controlID;
            oDropDownList.CssClass = this.TextBoxCssClass;
            oDropDownList.Width = width;
            foreach (string item in values) { oDropDownList.Items.Add(item); }
            oDropDownList.SelectedIndex = 0;
            controlCell.Controls.Add(oDropDownList);

            row.Cells.Add(labelCell);
            row.Cells.Add(controlCell);

            return row;
        }

        string GetText(string textBoxID)
        {
            TextBox textBox = (TextBox)this.FindControl(textBoxID);
            if (!String.IsNullOrEmpty(textBox.Text)) return textBox.Text;
            else return String.Empty;
        }

        void SetText(string textBoxID, string text)
        {
            TextBox textBox = (TextBox)this.FindControl(textBoxID);
            if (textBox != null)
            {
                textBox.Text = text;
            }
            else
                textBox.Text = "";
        }

        private bool GetCheckBox(string CheckBoxID)
        {
            CheckBox checkBox = (CheckBox)this.FindControl(CheckBoxID);
            return checkBox.Checked;
        }

        private void SetCheckBox(string CheckBoxID, bool Checked)
        {
            CheckBox checkBox = (CheckBox)this.FindControl(CheckBoxID);
            if (checkBox != null) { checkBox.Checked = Checked; }
            else { checkBox.Checked = false; }
        }

        private void SetTextBoxReadOnlyOption(string textBoxID, bool readOnly)
        {
            TextBox textBox = (TextBox)this.FindControl(textBoxID);
            textBox.ReadOnly = readOnly;
        }

        private String GetDropDownListText(String textBoxID)
        {
            DropDownList dropDownList = (DropDownList)this.FindControl(textBoxID);
            return dropDownList.Text ?? null;
        }

        private int GetDropDownListSelectedIndex(String textBoxID)
        {
            DropDownList dropDownlist = (DropDownList)this.FindControl(textBoxID);
            return dropDownlist.SelectedIndex;
        }

        private void SetDropDownListText(String textBoxID, String text)
        {
            DropDownList dropDownList = (DropDownList)this.FindControl(textBoxID);
            if (dropDownList != null) { dropDownList.Text = text; }
            else { dropDownList.Text = ""; }
        }

        private void SetDropDownListIndex(String textBoxID, int index)
        {
            DropDownList dropDownList = (DropDownList)this.FindControl(textBoxID);
            if (dropDownList != null) { dropDownList.SelectedIndex = index; }
        }

        #endregion
    }
}
